package util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import sun.swing.FilePane;

public class ImageFileChooser extends JFileChooser {
	private static final long serialVersionUID = 1L;
	private PropertyChangeListener listener;

	public PropertyChangeListener getListener() {
		return listener;

	}

	public ImageFileChooser() {
		this(null);
	}

	public ImageFileChooser(String path) {
		super(path);
		final ImageFileChooserBorderPanel preview = new ImageFileChooserBorderPanel();
		preview.setPreferredSize(new Dimension(250, 250));
		this.setPreferredSize(new Dimension(800, 400));
		setAccessory(preview);

		addPropertyChangeListener(this.listener = new PropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent e) {
				final ImageIcon ICON_EMPTY = new ImageIcon(
						"./src/ui/images/thumb_empty.jpg");
				final ImageIcon ICON_EMPTY_DEFAULT = new ImageIcon(
						"./src/ui/images/thumb_empty_default.jpg");
				String propertyName = e.getPropertyName();				
				if (propertyName
						.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
					File selection = (File) e.getNewValue();

					String name;				
					
					if (selection == null) {	
						preview.setImage(ICON_EMPTY_DEFAULT.getImage());
						return;		
					} else {
						name = selection.getAbsolutePath();
						ImageIcon icon;
						Image newImage;
						if (selection.length() > 6000000) {
							icon = ICON_EMPTY;

						} else {
							icon = new ImageIcon(name);
						}
						newImage = icon.getImage();
						preview.setImage(newImage);
					}
				} else if (propertyName.equals("SetDefaultThumb")) {
					preview.setImage(ICON_EMPTY_DEFAULT.getImage());
				}
			}
		});

	}

	@Override
	public void updateUI() {
		LookAndFeel old = UIManager.getLookAndFeel();
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable ex) {
			old = null;
		}

		super.updateUI();

		if (old != null) {
			FilePane filePane = findFilePane(this);
			filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
			filePane.setViewType(FilePane.VIEWTYPE_LIST);

			Color background = UIManager.getColor("Label.background");
			setBackground(background);
			setOpaque(true);

			try {
				UIManager.setLookAndFeel(old);
			} catch (UnsupportedLookAndFeelException ignored) {
			} // shouldn't get here
		}
	}

	private static FilePane findFilePane(Container parent) {
		for (Component comp : parent.getComponents()) {
			if (FilePane.class.isInstance(comp)) {
				return (FilePane) comp;
			}
			if (comp instanceof Container) {
				Container cont = (Container) comp;
				if (cont.getComponentCount() > 0) {
					FilePane found = findFilePane(cont);
					if (found != null) {
						return found;
					}
				}
			}
		}

		return null;
	}

}